package com.mz.ozy.widget.dialog;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.mz.ozy.R;
import com.mz.ozy.bean.C2cTradeBean;
import com.mz.ozy.utils.StringUtils;
import com.mz.ozy.utils.Utils;

import java.math.BigDecimal;

public class SellCardDialog extends BaseDialog implements View.OnClickListener {

    private EditText mEtDescribeMsg;
    private TextView mTvDescribeMsg;
    private TextView mTvGoodsPrice;
    private TextView mTvBalance;
    private TextView mTvBuy;
    private TextView mTvPayUsdt;
    private TextView mTvPayOutline;
    private TextView mTvExchange;
    private TextView mTvKtUnit;

    private String ktbalance;
    private String price;
    private String type = "0";
    private String ktType = "1";
    private final String usdtByCNY;
    C2cTradeBean.PageBean.PageItemsBean data;

    public SellCardDialog(Activity mContext, String type, String usdtByCNY, C2cTradeBean.PageBean.PageItemsBean data) {
        super(mContext, R.layout.dialog_sell_card, Gravity.CENTER, true, false);
        this.data = data;
        this.ktType = type;
        this.usdtByCNY = usdtByCNY;
        initView();
    }

    private void initView() {
        setOnViewClick(R.id.tv_cancel, this);
        setOnViewClick(R.id.tv_submit, this);
        setOnViewClick(R.id.tv_pay_usdt, this);
        setOnViewClick(R.id.tv_pay_outline, this);
        mTvBuy = getView(R.id.tv_buy);
        mTvGoodsPrice = getView(R.id.tv_goods_price);
        mTvBalance = getView(R.id.tv_balance);
        mTvPayUsdt = getView(R.id.tv_pay_usdt);
        mTvPayOutline = getView(R.id.tv_pay_outline);
        mEtDescribeMsg = getView(R.id.et_describe_msg);
        mTvDescribeMsg = getView(R.id.tv_describe_msg);
        mTvExchange = getView(R.id.tv_exchange);
        mTvKtUnit = getView(R.id.tv_kt_unit);
        mTvGoodsPrice.setText(StringUtils.formattingDecimal(data.getSellPrice(), 2));
        if ("1".equals(ktType)) {
            mTvDescribeMsg.setText("购买KT");
            mTvBuy.setText(mContext.getString(R.string.sysl, data.getCansell() + "KT"));
        } else {
            mTvDescribeMsg.setText("购买股权积分");
            mTvBuy.setText(mContext.getString(R.string.sysl, data.getCansell() + "股权积分"));
        }
        if ("1".equals(ktType)) {
            mTvKtUnit.setText("KT");
        } else {
            mTvKtUnit.setText("股权积分");
        }
        mEtDescribeMsg.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (Utils.editTextUtils(s, mEtDescribeMsg)) {
                    if ("0".equals(type)) {
                        if (!TextUtils.isEmpty(s.toString()) && !s.toString().equals(".")) {
                            mTvExchange.setText(Utils.decimalUtils(new BigDecimal(data.getSellPrice()).multiply(new BigDecimal(s.toString())).divide(new BigDecimal(usdtByCNY), 4) + "") + "USDT");
                        } else {
                            mTvExchange.setText("0.0000USDT");
                        }
                    } else {
                        if (!TextUtils.isEmpty(s.toString()) && !s.toString().equals(".")) {
                            mTvBalance.setText(Utils.decimalUtils(new BigDecimal(data.getSellPrice()).multiply(new BigDecimal(s.toString())) + "", 2) + "CNY");
                        } else {
                            mTvBalance.setText("0.00CNY");
                        }
                    }
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:
                if (mListener != null) {
                    mListener.onCancel();
                }
                break;
            case R.id.tv_submit:
                if (mListener != null) {
                    String msg = mEtDescribeMsg.getText().toString().trim();
                    mListener.onSubmit(msg, type);
                }
                break;
            case R.id.tv_pay_usdt: {
                type = "0";
                mTvPayUsdt.setBackgroundResource(R.drawable.bg_solid_519bff_2dp);
                mTvPayUsdt.setTextColor(ContextCompat.getColor(mContext, R.color.white));
                mTvPayOutline.setBackgroundResource(R.drawable.bg_stroke_cccccc_2dp);
                mTvPayOutline.setTextColor(ContextCompat.getColor(mContext, R.color.c_8f969e));
                final String ktNum = mEtDescribeMsg.getText().toString().trim();
                if (!TextUtils.isEmpty(ktNum)) {
                    mTvExchange.setText(Utils.decimalUtils(new BigDecimal(data.getSellPrice()).multiply(new BigDecimal(ktNum)).divide(new BigDecimal(usdtByCNY), 4) + "") + "USDT");
                } else {
                    mTvExchange.setText("0.0000USDT");
                }
                mTvBalance.setText("0.00CNY");
                break;
            }
            case R.id.tv_pay_outline: {
                type = "1";
                mTvPayUsdt.setBackgroundResource(R.drawable.bg_stroke_cccccc_2dp);
                mTvPayUsdt.setTextColor(ContextCompat.getColor(mContext, R.color.c_8f969e));
                mTvPayOutline.setBackgroundResource(R.drawable.bg_solid_519bff_2dp);
                mTvPayOutline.setTextColor(ContextCompat.getColor(mContext, R.color.white));
                final String ktNum = mEtDescribeMsg.getText().toString().trim();
                if (!TextUtils.isEmpty(ktNum)) {
                    mTvBalance.setText(Utils.decimalUtils(new BigDecimal(data.getSellPrice()).multiply(new BigDecimal(ktNum)) + "", 2) + "CNY");
                } else {
                    mTvBalance.setText("0.00CNY");
                }
                mTvExchange.setText("0.0000USDT");
                break;
            }
        }
    }

    private onDialogListener mListener;

    public void setOnDialogListener(onDialogListener listener) {
        mListener = listener;
    }

    public interface onDialogListener {
        void onCancel();

        void onSubmit(String msg, String type);
    }
}
